package server.business.utils;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @auther MingHsZzz
 * @Date 2023/3/20  17:05
 * @project shenghuo
 * @description
 */
public class GenerateSql {

   public static Connection getConnection() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, SQLException {
      // 使用反射动态加载mysql驱动件程序
      Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
      Driver driver = (Driver) aClass.newInstance();
      // 创建Properties对象，用于保存mysql账号和密码键值对
      Properties properties = new Properties();
      properties.setProperty("user", "root");
      properties.setProperty("password", "123456");
      String url = "jdbc:mysql://localhost:3306/20230317";
      // 得到mysql的连接
      Connection connection = driver.connect(url, properties);
      // 得到可以与mysql语句进行交互的对象
//        Statement statement = connection.createStatement();
//        // 关闭与 mysql语句进行交互的对象
//        statement.close();
//        // 关闭与 mysql语句进行交互的对象
//        connection.close();
      return connection;
   }

   static void testjdbc() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
      Connection connection = getConnection();
      Statement statement = connection.createStatement();

      String Sql = "select * from function_page where id > 9";

      ResultSet execute = statement.executeQuery(Sql);
      StringBuffer stringBuffer = new StringBuffer();
      //"UPDATE function_page SET url='"
      List<String> list = new ArrayList<>();
      while(execute.next()){

//         System.out.println(execute.getString("url"));
         String[] urls = execute.getString("url").split("/");
         list.add("UPDATE function_page SET url='" + urls[urls.length - 1].split("\\.")[0] +"' WHERE id = " + execute.getInt("id"));
      }
//      System.out.println(list);
      for (String s : list) {
         System.out.println(s);
         boolean execute1 = statement.execute(s);
         System.out.println(execute1);
      }

      statement.close();
      connection.close();
   }

   static void insertpage() {


   }

   static void getUrl() throws FileNotFoundException {
//      StringBuilder stringBuilder = new StringBuilder();
//      StringBuilder preStringBuilder = new StringBuilder();
//      StringBuilder postStringBuilder = new StringBuilder();
//      System.out.println(preStringBuilder);
      File file = new File("D:\\important\\项目文件\\输血项目\\code\\20230317\\main\\src\\main\\resources\\templates\\pages\\after");

      FileOutputStream fileOutputStream = new FileOutputStream(new File("test.java"));

      for (File listFile : file.listFiles()) {
         if (listFile.isDirectory()) {
            if (listFile.getName().equals("zy") || listFile.getName().equals("fzx")) {
               for (File file1 : listFile.listFiles()) {
                  for (File file2 : file1.listFiles()) {

                     String[] split1 = file2.getName().split("\\.");

                     String testgoal = "    @RequestMapping(\"/" + split1[0] + "\")\n" +
                             "    public String to"+split1[0]+"(){\n" +
                             "        return \"pages/after/"+ listFile.getName()+"/"+file1.getName()+"/"+split1[0] +"\";\n" +
                             "    }";

                     System.out.println(testgoal);
                  }
               }

            } else
               for (File file1 : listFile.listFiles()) {
//                        System.out.println(listFile.getName());
//                        System.out.println(file1);
//                        String[] split = file1.getPath().split("/");
                  String[] split1 = file1.getName().split("\\.");

                  String testgoal = "    @RequestMapping(\"/" + split1[0] + "\")\n" +
                          "    public String to"+ split1[0]+"(){\n" +
                          "        return \"pages/after/"+ listFile.getName()+"/"+split1[0] +"\";\n" +
                          "    }";
                  System.out.println(testgoal);

               }
         }
      }


   }


   public static void main(String[] args) {

      try {
         testjdbc();
      } catch (SQLException e) {
         throw new RuntimeException(e);
      } catch (ClassNotFoundException e) {
         throw new RuntimeException(e);
      } catch (InstantiationException e) {
         throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
         throw new RuntimeException(e);
      }

   }

}
